package com.example.dbmysql.Controller;


import com.example.dbmysql.pojo.Channel;
import com.example.dbmysql.result.Result;
import com.example.dbmysql.service.channelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/channel")
public class channelController {

    @Autowired
    private channelService channelService;


    @PostMapping("/add")
    public Result add(@RequestBody Channel channel){

        Channel chan =  channelService.selectByRoomid(channel.getChannelId());

        if(chan != null) return  Result.error("该通道已存在");

        channelService.add(channel);

        return  Result.success();

    }


    @GetMapping("/getAll")
    public Result getAll(){

        List<Channel> list =  channelService.selectAll();

        return Result.success(list);
    }


    @PutMapping("/update")
    public Result update(@RequestBody Channel channel){

        System.out.println(channel);

        channelService.update(channel);

        return  Result.success();
    }

    @DeleteMapping("/del/{id}")
    public Result delById(@PathVariable("id") Integer id){

        channelService.delById(id);

        return Result.success();
    }

    @GetMapping("/getByid/{id}")
    public Result getById(@PathVariable("id") Integer id){

        Channel channel = channelService.getById(id);

        return Result.success(channel);
    }

}
